<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    ul{
        width: 600px;
        border: 1px solid black;
        margin: 0;
        padding: 0
    }
    li{
        width: 400px;
        background: cadetblue;
        margin-bottom: 2px;
        list-style: none;
    }
    .active{
        background: red;
    }
    </style>
</head>
<body>
    <ul>
        <input type="button" value="增加li标签">
        <li class="active"><span>span1</span></li>
        <li>02</li>
        <li>03</li>
        <li>04</li>
        <li>05</li>
        <li>06</li>
        <li>07</li>
        <li>08</li>
        <li>09</li>
        <li>10</li>
    </ul>
    <script>
    var oparent=document.querySelector("ul")
    var listli=document.getElementsByTagName("li")
    var obt=document.querySelector("input[type='button']")
    oparent.onclick=function(e){
        if(e.target.tagName=="LI"){
            for(var i=0;i<listli.length;i++){
                listli[i].className=""
            }
            e.target.className="active"
            e.target.style.color=getColor()
        }
    }
    var a=10;
    obt.onclick=function(){
        var li=document.createElement("li")
        a++;
        li.innerHTML=a
        oparent.appendChild(li)
    }

    function getColor(){
  var r=Math.round(Math.random()*255)
  var g=Math.round(Math.random()*255)
  var b=Math.round(Math.random()*255)
    return "rgb("+r+","+g+","+b+")"
}

    // for(var i=0;i<listli.length;i++){
    //     listli[i].onclick=function(){
    //         for(var j=0;j<listli.length;j++){
    //             listli[j].className=""
    //         }
    //         this.className="active"
    //     }
    // }
    </script>
</body>
</html>